package com.banciyuan.bcywebview.biz.privatemessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2016/9/9.
 */
public class PrivateMessageListActivity extends BaseActivity implements ActionbarHelper.OnActionCallbacks {

    private PrivateMessageFragment receiveFragment;
    private PrivateMessageFragment sendFragment;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<Fragment> flist = new ArrayList<>();
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private String[] tabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privatemessage_list);
        initArgs();
        getFragment();
        initActionbar();
        initUi();
        initData();
        initAction();
    }

    @Override
    protected void initArgs() {
        tabs = new String[]{getString(R.string.receive_mail), getString(R.string.send_mail)};
    }

    private void getFragment() {

        Bundle receive = new Bundle();
        Bundle send = new Bundle();
        receive.putInt("type", 0);
        send.putInt("type", 1);

        receiveFragment = new PrivateMessageFragment();
        receiveFragment.setArguments(receive);
        sendFragment = new PrivateMessageFragment();
        sendFragment.setArguments(send);
        flist.add(receiveFragment);
        flist.add(sendFragment);
    }

    @Override
    protected void initUi() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.pager);
    }


    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.private_message));
        mActionbarHelper.addItemOneText(getString(R.string.setting));
        mActionbarHelper.setActionCallbacks(this);
    }


    @Override
    protected void initData() {

        FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return flist.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        };
        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public void onBackClick() {
        if (receiveFragment != null) {
            Intent intent = new Intent();
            intent.putExtra("allclear", receiveFragment.getAllClear());
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        if (receiveFragment != null) {
            Intent intent = new Intent();
            intent.putExtra("allclear", receiveFragment.getAllClear());
            setResult(RESULT_OK, intent);
        }
        super.onBackPressed();
    }

    @Override
    public void onActionClick(int id) {
        switch (id) {
            case R.id.base_action_bar_home_text_item:
                gotoUtil.gotoAct(this, PrivateMessageSettingActivity.class);
                break;
        }
    }
}
